#!/bin/env python3
import ithildin as ith
import numpy as np
import matplotlib.pyplot as plt
import sys
from typing import List
from scipy.interpolate import RectBivariateSpline
from pydiffmap.diffusion_map import DiffusionMap
from pydiffmap import visualization as diff_visualization
import matplotlib
import matplotlib.cm as cm

matplotlib.use("TkAgg")
courtemanche_spiral = ith.SimData.from_stem("myokit_CMspiral3/myokit_5")

# 70, 74, 78
for i in range(70,79,4):
    print(i)
    frame = courtemanche_spiral.vars['u'][i,0,:,:]
    plt.imshow(frame)
    plt.savefig("CMspiral3-z0-t%s.pdf" % (i,))
    plt.savefig("CMspiral3-z0-t%s.png" % (i,))
    plt.show()
    # todo assen benoemen
    plt.close()

# Show two frames of courtemanche_spiral, with a single colourbar,
# as described in <https://stackoverflow.com/questions/13784201/how-to-have-one-colorbar-for-all-subplots/23795901#23795901>
fig,axes = plt.subplots(ncols=2, constrained_layout=True, sharex=True, sharey=True)
fig.supxlabel(r"$x$[mm]",y=0.16)
fig.supylabel(r"$y$[mm]")
frame70 = courtemanche_spiral.vars['u'][70,0,:,:]
frame74 = courtemanche_spiral.vars['u'][74,0,:,:]
vmin = min([np.min(frame70),np.min(frame74)]) # lowest value for colour bar
vmax = min([np.max(frame70),np.max(frame74)]) # highest value for colour bar
im = axes.flat[0].imshow(frame70, vmin=vmin, vmax=vmax)
im = axes.flat[1].imshow(frame74, vmin=vmin, vmax=vmax)
fig.colorbar(im, ax=axes.ravel().tolist(),shrink=0.5,label='u[A.U]')
plt.savefig("CMspiral3-z0-t70-and-74.pdf",bbox_inches='tight')
plt.savefig("CMspiral3-z0-t70-and-74.png",bbox_inches='tight')
plt.show()
plt.close()

ap = ith.SimData.from_stem("phase_1000/phase_1000")
np.random.seed(1)
u = ap.vars['u'][100:,:,20:380,20:380].ravel()
v = ap.vars['v'][100:,:,20:380:,20:380].ravel()
points = np.random.choice(u.size,2500000)
u = u[points]
v = v[points]
plt.xlabel("u[A.U]")
plt.ylabel("v[A.U]")
plt.scatter(u,v,s=0.001)
plt.savefig("phase1000-fase.png")
plt.show()
plt.close()

# Make a standalone colourbar for the thesis.
bofc = ith.SimData.from_stem("results_9010/PDL_9010")
fig, ax = plt.subplots(figsize=(1,3))
fig.subplots_adjust(right=0.25)
bofc_vmin = np.min(bofc.vars['s'])
bofc_vmax = np.min(bofc.vars['s'])
fig.colorbar(cm.ScalarMappable(cmap='viridis',norm=matplotlib.colors.Normalize(vmin=bofc_vmin,vmax=bofc_vmax)),orientation="vertical",cax=ax)
plt.savefig("bofc-transformed-4th-standalone-colourbar.png",bbox_inches='tight')
plt.savefig("bofc-transformed-4th-standalone-colourbar.pdf",bbox_inches='tight')
plt.show()
plt.close()

# TODO check, actually run
# Not very fast but < a minute
np.random.seed(1)
# Vermijd randeffecten en initialiatie
def snoei(variable):
	print(np.shape(variable))
	# see dimensiereductie_BOFC_verbeterd.py
	return variable[(41 * 7 + 32)//33:,2:18,375//8:322,375//8:322].ravel()
u = snoei(bofc.vars['u'])
points = np.random.choice(u.size,500000)
                                #2500000
u = u[points]
v = snoei(bofc.vars['v'])[points]
w = snoei(bofc.vars['w'])[points]
s = snoei(bofc.vars['s'])[points]

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.scatter(u,v,w,c=s,cmap='viridis',s=0.002) #,cmap='viridis') #plt.hot(),s=0.002)
ax.set_xlabel(r'$u$')
ax.set_ylabel(r'$v$')
ax.set_zlabel(r'$w$')
#plt.colorbar() TODO
fig.gca().set_title(r'$s$')
plt.savefig("BOFC-fase-5000.png")
plt.show()
plt.close()
